package com.zcckj.demo.pay.provider;

import com.zcckj.demo.pay.model.dto.ReduceBalanceDTO;
import com.zcckj.demo.pay.model.entity.Account;
import com.zcckj.demo.pay.service.AccountService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

/**
 *
 * <p>
 *  TODO 类作用描述
 * </p>
 *
 * @author zhangjie
 * @since 2019-08-27
 */
@SuppressWarnings("Duplicates")
@Service(group = "${dubbo.services.group}",version = "${dubbo.services.version}")
public class PayWriteServiceImpl implements PayWriteService {
    @Autowired
    private AccountService accountService;

    @Override
    public Boolean reduceBalance(ReduceBalanceDTO reduceBalanceDTO) {
        Assert.notNull(reduceBalanceDTO.getUserId(),"userId cannot be null");
        Assert.notNull(reduceBalanceDTO.getPrice(),"price cannot be null");
        Account account = accountService.getById(reduceBalanceDTO.getUserId());
        if(account.getBalance().compareTo(reduceBalanceDTO.getPrice()) < 0 ){
            throw new RuntimeException("余额不足");
        }
        account.setBalance(account.getBalance()-reduceBalanceDTO.getPrice());
        return accountService.updateById(account);
    }
}
